package com.lyb.ui.controller;

import com.lyb.constant.DiskConstant;
import com.lyb.constant.SystemConstant;
import com.lyb.ui.model.DiskContent;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.util.Duration;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 磁盘页面的控制器
 *
 * @author Dayun Mr.Alpaca
 * @version 1.4.0
 */
public class DiskController implements Initializable {
    @FXML
    public Label sumSizeSign;
    @FXML
    public Label sumSizeLabel;
    @FXML
    public Label usedSizeSign;
    @FXML
    public Label usedSizeLabel;
    @FXML
    public Label emptySizeSign;
    @FXML
    public Label emptySizeLabel;

    @FXML
    private TableView<DiskContent> diskTable;
    @FXML
    private TableColumn<?, ?> diskId;
    @FXML
    private TableColumn<?, ?> diskSize;
    @FXML
    private TableColumn<?, ?> usedSpace;
    @FXML
    private TableColumn<?, ?> freeSpace;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        /* 组件初始化操作 */
        init(new String[] {"diskId", "diskSize", "usedSpace", "freeSpace"}, diskId, diskSize, usedSpace, freeSpace);

        /* 获取组件的动画事件 */
        KeyFrame sizeFrame = getSizeFrame();
        KeyFrame tableFrame = getTableFrame();

        /* 添加动画事件到时间线 */
        Timeline timeline = new Timeline();
        timeline.getKeyFrames().addAll(sizeFrame, tableFrame);
        timeline.setCycleCount(Timeline.INDEFINITE);
        timeline.setAutoReverse(true);
        timeline.play();
    }

    /**
     * 获取空间组件的动画事件
     *
     * @return 事件
     */
    private KeyFrame getSizeFrame() {
        sumSizeLabel.setText(DiskConstant.DISK_LENGTH + " 字节");
        return new KeyFrame(Duration.millis(500), event -> {
            int emptySize = SystemConstant.MAIN_BOARD.getEmptySizeFromDisk();
            emptySizeLabel.setText(emptySize + " 字节");
            usedSizeLabel.setText(DiskConstant.DISK_LENGTH - emptySize + " 字节");
        });
    }

    /**
     * 获取表格组件的动画事件
     *
     * @return 事件
     */
    private KeyFrame getTableFrame() {
        return new KeyFrame(Duration.millis(1000), event -> {
            List<DiskContent> diskList = new ArrayList<>();
            byte[] fat = SystemConstant.MAIN_BOARD.getFatFromDisk();
            for (int i = 0; i < DiskConstant.BLOCKS_MAX_ASSOCIATION_NUM; i++) {
                String diskId = i + 1 + " 号";
                String diskSize = DiskConstant.DISKBLOCK_LENGTH + " 字节";
                int usedSize = (fat[i] == 0 ? 0 : DiskConstant.DISKBLOCK_LENGTH);
                int freeSize = DiskConstant.DISKBLOCK_LENGTH - usedSize;
                DiskContent disk = new DiskContent(diskId, diskSize, usedSize + " 字节", freeSize + " 字节");
                diskList.add(disk);
            }
            ObservableList<DiskContent> disks = FXCollections.observableList(diskList);
            diskTable.setItems(disks);
        });
    }

    /**
     * 初始化操作，禁止组件调整、排序、编辑、扩容
     * 并将数据关联到具体列
     *
     * @param propertys 关联的列
     * @param tables 组件
     */
    private void init(String[] propertys, TableColumn<?, ?>... tables) {
        int i = 0;
        for (TableColumn<?, ?> table : tables) {
            table.setResizable(false);
            table.setSortable(false);
            table.setEditable(false);
            table.setResizable(false);
            table.setCellValueFactory(new PropertyValueFactory<>(propertys[i++]));
        }
    }
}
